package com.sunyzc.ebaotech.simulator;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sunyzc.ebaotech.simulator.ybt.YBTClientConfig;

@SuppressWarnings("unchecked")
public class SimulatorConfig {
	private static final Logger log = LoggerFactory.getLogger(SimulatorConfig.class);
	private static final Element frameConfig = loadFrameConfig();
	private static final Map<String, YBTClientConfig> ybtClientConfigMap = loadClientConfigMap();
	private static final Vector<JComboBoxItem> ybtServerHostItems = loadYBTServerHostItems();
	private static final Vector<JComboBoxItem> soapServerHostItems = loadSOAPServerHostItems();
	private static final Vector<JComboBoxItem> clientTransactionItems = loadClientTransactionItems();
	private static final Vector<JComboBoxItem> chanelTransactionItems = loadChannelTransactionItems();

	public static Map<String, YBTClientConfig> loadClientConfigMap() {
		if (ybtClientConfigMap != null)
			return ybtClientConfigMap;
		Map<String, YBTClientConfig> ybtClientConfigMap = new HashMap<String, YBTClientConfig>();
		List<Element> serverElementList = frameConfig.element("YBT").elements("client");
		for (Element serverElement : serverElementList)
			ybtClientConfigMap.put(serverElement.attributeValue("clientCode"), new YBTClientConfig(serverElement.attributeValue("clientCode"), serverElement.attributeValue("clientName"), Integer.parseInt(serverElement.attributeValue("port")), serverElement.attributeValue("transEncoding"), Boolean.parseBoolean(serverElement.attributeValue("needCipher"))));
		return ybtClientConfigMap;
	}

	public static Vector<JComboBoxItem> loadYBTServerHostItems() {
		if (ybtServerHostItems != null)
			return ybtServerHostItems;
		Vector<JComboBoxItem> ybtServerHostItems = new Vector<JComboBoxItem>();
		List<Element> serverElementList = frameConfig.element("YBT").elements("server");
		for (Element serverElement : serverElementList)
			ybtServerHostItems.add(new JComboBoxItem(serverElement.attributeValue("host"), serverElement.attributeValue("hostDesc")));
		return ybtServerHostItems;
	}

	public static Vector<JComboBoxItem> loadSOAPServerHostItems() {
		if (soapServerHostItems != null)
			return soapServerHostItems;
		Vector<JComboBoxItem> soapServerHostItems = new Vector<JComboBoxItem>();
		List<Element> serverElementList = frameConfig.element("SOAP").elements("server");
		for (Element serverElement : serverElementList)
			soapServerHostItems.add(new JComboBoxItem(serverElement.attributeValue("host"), serverElement.attributeValue("hostDesc")));
		return soapServerHostItems;
	}

	public static Vector<JComboBoxItem> loadClientTransactionItems() {
		if (clientTransactionItems != null)
			return clientTransactionItems;
		Vector<JComboBoxItem> clientTransactionItems = new Vector<JComboBoxItem>();
		List<Element> clientElementList = frameConfig.element("YBT").elements("client");
		for (Element clientElement : clientElementList) {
			JComboBoxItem clientItem = new JComboBoxItem(clientElement.attributeValue("clientCode"), clientElement.attributeValue("clientName"));
			String extendsClient = clientElement.attributeValue("extends");
			if (extendsClient != null && !extendsClient.trim().equals(""))
				for (JComboBoxItem parentClientItem : clientTransactionItems)
					if (extendsClient.equals(parentClientItem.getKey()))
						clientItem.getChildrenComboBoxItem().addAll(parentClientItem.getChildrenComboBoxItem());
			for (Element transactionElement : (List<Element>) clientElement.elements()) {
				JComboBoxItem transactionItem = new JComboBoxItem(transactionElement.attributeValue("requestTransType"), transactionElement.attributeValue("transTypeDesc"));
				if (clientItem.getChildrenComboBoxItem().contains(transactionItem))
					clientItem.getChildrenComboBoxItem().remove(transactionItem);
				clientItem.getChildrenComboBoxItem().add(transactionItem);
			}
			clientTransactionItems.add(clientItem);
		}
		return clientTransactionItems;
	}

	public static Vector<JComboBoxItem> loadChannelTransactionItems() {
		if (chanelTransactionItems != null)
			return chanelTransactionItems;
		Vector<JComboBoxItem> chanelTransactionItems = new Vector<JComboBoxItem>();
		List<Element> clientElementList = frameConfig.element("SOAP").elements("channel");
		for (Element clientElement : clientElementList) {
			JComboBoxItem clientItem = new JComboBoxItem(clientElement.attributeValue("channelCode"), clientElement.attributeValue("channelName"));
			for (Element transactionElement : (List<Element>) clientElement.elements())
				clientItem.getChildrenComboBoxItem().add(new JComboBoxItem(transactionElement.attributeValue("requestTransType"), transactionElement.attributeValue("transTypeDesc")));
			chanelTransactionItems.add(clientItem);
		}
		return chanelTransactionItems;
	}

	private static Element loadFrameConfig() {
		Element frameConfig = null;
		InputStream inputStream = null;
		try {
			inputStream = ClientFrame.class.getResourceAsStream("/simulatorConfig.xml");
			frameConfig = new SAXReader().read(inputStream).getRootElement();
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			IOUtil.closeQuietly(inputStream);
		}
		return frameConfig;
	}
}
